import os

from fastapi import UploadFile
from utils.utils import get_uid
import datetime


async def save_file_to_temp(file: UploadFile):
    """
    save file to temp
    :param file:
    :return:
    """

    file_path = "temp/{}.{}".format(get_uid(), file.filename.rsplit(".")[-1])
    with open(file_path, 'wb') as f:
        f.write(await file.read())
    return file_path


def get_cos_file_path(file_path: str, user="userself"):
    file_name = file_path.rsplit("/", 1)[-1]
    suffix = file_name.rsplit(".", 1)[-1]

    return "/{user}/{year}-{month}/{suffix}/{file_name}".format(user=user,
                                                                year=datetime.date.today().year,
                                                                month=datetime.date.today().month,
                                                                suffix=suffix,
                                                                file_name=file_name)


def del_temp_file(file_path):
    os.remove(file_path)
